// Learn cc.Class:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
var AU = require("AudioUtils").inst;
let self;
cc.Class({
    extends: cc.Component,

    properties: {
		musicSolid:{//音乐滑块
			default: null,
			type: cc.Slider
		},
		effectSolid:{//音效滑块
			default: null,
			type: cc.Slider
		},
		musicChk:{//音乐开关
			default: null,
			type: cc.Toggle
		},
		effectChk:{//音效开关
			default: null,
			type: cc.Toggle
		},
		musicVoiceProgress:{
			default: null,
			type: cc.ProgressBar
		},
		effectVoiceProgress:{
			default: null,
			type: cc.ProgressBar
		},
	},

    _init: function(){
    	debugger
    	var m = AU.getMusicVolume1();//回显存储本地音量(音乐)大小
    	self.musicSolid.progress = m;
		self._updateMusicVolume(m);
		self.musicChk.isChecked = !AU.getSwitchMusic();//设置音乐开关
		
		var e = AU.getEffectVolume1();//回显存储本地音量(音效)大小
    	self.effectSolid.progress = e;
		self._updateEffectVolume(e);
		self.effectChk.isChecked = !AU.getSwitchEffect();//设置音效开关
	},
	
    onLoad () {
    	self = this;
    	//console.log("soundsetup init...........................");
    	//self._init();
    },

 	_updateMusicVolume: function(progress) {
        AU.setMusicVolume(progress);
        //console.log(progress);
    },
    onSliderMusicEvent: function(sender, eventType) {
        self._updateMusicVolume(sender.progress);
        self.musicVoiceProgress.progress = sender.progress;
    },
    
    _updateEffectVolume: function(progress) {
        AU.setEffectVolume(progress);
    },
    onSliderEffectEvent: function(sender, eventType) {
        self._updateEffectVolume(sender.progress);
        self.effectVoiceProgress.progress = sender.progress;
    },
    
    /**
     * 音乐开关
     * @param {Object} title
     * @param {Object} label
     * @param {Object} toggle
     */
    onMusicToggleEvent: function(toggle) {
    	//console.log("toggle:"+toggle.isChecked);
        if(toggle.isChecked) {//由于UI原因,isChecked为true则代表关闭
            AU.switchMusicFunc(false);
        } else {
        	AU.switchMusicFunc(true);
            if(!AU.isMusicPlaying()){//重新打开游戏后,打开声音执行setResumeMusic()恢复播放无效,需要重新播放
            	AU.playMusic(MUSIC_SPACE_TRIP,true);
            }
        }
    },
    
    /**
     * 音效开关
     * @param {Object} title
     * @param {Object} label
     * @param {Object} toggle
     */
    onEffectToggleEvent: function(toggle) {
    	//console.log("toggle:"+toggle.isChecked);
        if(toggle.isChecked) {//由于UI原因,isChecked为true则代表关闭
            AU.switchEffectFunc(false);
        } else {
            AU.switchEffectFunc(true);
        }
    },
    
    start () {
		self._init();
    },

    // update (dt) {},
});
